<?php
class ModelCommunityUserGroup extends Model {
    public function editUserGroups($data) {
        $role_id_query=$this->db->query("SELECT role_id FROM " . DB_PREFIX . "community_role ORDER BY role_id DESC LIMIT 0,1");
        $new_role_id_start=$role_id_query->row['role_id']+1;

        $rank_id_str='';

        foreach($data['member-rank'] as $member_rank){
            if(!empty($member_rank['rank_id'])){
                $rank_id_str.=$member_rank['rank_id'] . ',';
            }
        }

        $rank_id_str=rtrim($rank_id_str,',');

        //get rank role permission
        if(!empty($rank_id_str)){
            $rank_role_permissions=array();

            $rank_role_permission_query=$this->db->query("SELECT * FROM " . DB_PREFIX . "community_role WHERE role_id='5' AND rank_id IN (" . $rank_id_str . ")");
            foreach($rank_role_permission_query->rows as $rank_role_permission){
                $rank_role_permissions[$rank_role_permission['rank_id']]=$rank_role_permission;
            }
        }

        //get rank permission
        if(!empty($rank_id_str)){
            $rank_permissions=array();

            $rank_permissions_query=$this->db->query("SELECT * FROM " . DB_PREFIX . "community_permission WHERE role_id='5' AND rank_id IN (" . $rank_id_str . ")");
            foreach($rank_permissions_query->rows as $rank_permission){
                $rank_permissions[$rank_permission['rank_id']][$rank_permission['forum_id']]=$rank_permission;
            }
        }

        //get user defined and pre-defined group role permission
        $group_role_permissions=array();
        $role_id_str='';

        foreach($data['user-defined-group'] as $user_defined_group){
            if(!empty($user_defined_group['role_id'])){
                $role_id_str.=$user_defined_group['role_id'] . ',';
            }
        }

        foreach($data['pre-defined-group'] as $pre_defined_group){
            if(!empty($pre_defined_group['role_id'])){
                $role_id_str.=$pre_defined_group['role_id'] . ',';
            }
        }

        $role_id_str=rtrim($role_id_str,',');

        if(!empty($role_id_str)){
            $group_role_permission_query=$this->db->query("SELECT * FROM " . DB_PREFIX . "community_role WHERE rank_id='0' AND role_id IN (" . $role_id_str . ")");

            foreach($group_role_permission_query->rows as $group_role_permission){
                $group_role_permissions[$group_role_permission['role_id']]=$group_role_permission;
            }
        }

        //get user-defined permission
        $permission_role_id_str='';
        $user_defined_group_permissions=array();

        foreach($data['user-defined-group'] as $user_defined_group){
            if(!empty($user_defined_group['role_id'])){
                $permission_role_id_str.=$user_defined_group['role_id'] . ',';
            }
        }

        $permission_role_id_str=rtrim($permission_role_id_str,',');

        if(!empty($permission_role_id_str)){
            $user_defined_group_permission_query=$this->db->query("SELECT * FROM " . DB_PREFIX . "community_permission WHERE rank_id='0' AND role_id IN (" . $permission_role_id_str . ")");

            foreach($user_defined_group_permission_query->rows as $user_defined_group_permission){
                $user_defined_group_permissions[$user_defined_group_permission['role_id']][$user_defined_group_permission['forum_id']]=$user_defined_group_permission;
            }
        }

        $this->db->query("DELETE FROM " . DB_PREFIX . "community_ranks");
        $this->db->query("DELETE FROM " . DB_PREFIX . "community_rank_title");
        $this->db->query("DELETE FROM " . DB_PREFIX . "community_role WHERE role_id='5' OR role_id>'7'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "community_role_title");
        $this->db->query("DELETE FROM " . DB_PREFIX . "community_permission WHERE role_id='5' OR role_id>'7'");

        foreach($data['member-rank'] as $member_rank){
            if(!empty($member_rank['rank_id'])){
                $this->db->query("INSERT INTO " . DB_PREFIX . "community_ranks SET rank_id='" . (int)$member_rank['rank_id'] . "', rank_min='" . (int)$member_rank['rank_min'] . "', rank_stars='" . (int)$member_rank['rank_stars'] . "'");

                //set user rank role permission
                $this->db->query("INSERT INTO " . DB_PREFIX . "community_role SET role_id='5', rank_id='" . (int)$member_rank['rank_id'] . "', posting_captcha='" . (int)$rank_role_permissions[$member_rank['rank_id']]['posting_captcha'] . "', user_ignore_flood_limit='" . (int)$rank_role_permissions[$member_rank['rank_id']]['user_ignore_flood_limit'] . "', allow_user_sig='" . (int)$rank_role_permissions[$member_rank['rank_id']]['allow_user_sig'] . "', allow_user_sig_bbcode='" . (int)$rank_role_permissions[$member_rank['rank_id']]['allow_user_sig_bbcode'] . "', allow_user_sig_bbcode_img='" . (int)$rank_role_permissions[$member_rank['rank_id']]['allow_user_sig_bbcode_img'] . "', allow_user_sig_bbcode_flash='" . (int)$rank_role_permissions[$member_rank['rank_id']]['allow_user_sig_bbcode_flash'] . "', allow_user_sig_bbcode_url='" . (int)$rank_role_permissions[$member_rank['rank_id']]['allow_user_sig_bbcode_url'] . "', allow_user_sig_smiles='" . (int)$rank_role_permissions[$member_rank['rank_id']]['allow_user_sig_smiles'] . "'");

                //set user rank permission
                foreach($rank_permissions[$member_rank['rank_id']] as $forum_id => $rank_permission){
                    $this->db->query("INSERT INTO " . DB_PREFIX . "community_permission SET forum_id='" . (int)$forum_id . "', role_id='5', rank_id='" . (int)$rank_permission['rank_id'] . "', view_forum='" . (int)$rank_permission['view_forum'] . "', topic_view='" . (int)$rank_permission['topic_view'] . "', topic_publish='" . (int)$rank_permission['topic_publish'] . "', topic_reply='" . (int)$rank_permission['topic_reply'] . "', post_delete='" . (int)$rank_permission['post_delete'] . "', post_edit='" . (int)$rank_permission['post_edit'] . "', poll_publish='" . (int)$rank_permission['poll_publish'] . "', poll_edit='" . (int)$rank_permission['poll_edit'] . "', poll_vote='" . (int)$rank_permission['poll_vote'] . "', posting_approval='" . (int)$rank_permission['posting_approval'] . "', posting_bbcode='" . (int)$rank_permission['posting_bbcode'] . "', posting_bbcode_img='" . (int)$rank_permission['posting_bbcode_img'] . "', posting_bbcode_flash='" . (int)$rank_permission['posting_bbcode_flash'] . "', posting_bbcode_url='" . (int)$rank_permission['posting_bbcode_url'] . "', posting_smiles='" . (int)$rank_permission['posting_smiles'] . "'");
                }

                $rank_id=$member_rank['rank_id'];
            }else{
                $this->db->query("INSERT INTO " . DB_PREFIX . "community_ranks SET rank_min='" . (int)$member_rank['rank_min'] . "', rank_stars='" . (int)$member_rank['rank_stars'] . "'");

                $rank_id=$this->db->getLastId();

                //set user rank role permission
                $this->db->query("INSERT INTO " . DB_PREFIX . "community_role SET role_id='5', rank_id='" . (int)$rank_id . "'");

                //set user rank permission
                foreach($this->getAllForumIds() as $result){
                    $this->db->query("INSERT INTO " . DB_PREFIX . "community_permission SET forum_id='" . (int)$result['forum_id'] . "', role_id='5', rank_id='" . (int)$rank_id . "'");
                }
            }

            foreach($member_rank['rank_title'] as $key => $rank_title){
                $this->db->query("INSERT INTO " . DB_PREFIX . "community_rank_title SET rank_id='" . (int)$rank_id . "', language_id='" . (int)$key . "', rank_title='" . $this->db->escape(trim($rank_title)) . "', rank_image='" . $this->db->escape(trim($member_rank['rank_image'][$key])) . "'");
            }
        }

        //user defined group
        foreach($data['user-defined-group'] as $user_defined_group){
            if(!empty($user_defined_group['role_id'])){
                //set user defined group role permission
                $this->db->query("INSERT INTO " . DB_PREFIX . "community_role SET role_id='" . (int)$user_defined_group['role_id'] . "', rank_id='0', member_ban='" . (int)$group_role_permissions[$user_defined_group['role_id']]['member_ban'] . "', member_warn='" . (int)$group_role_permissions[$user_defined_group['role_id']]['member_warn'] . "', member_disallow_posting='" . (int)$group_role_permissions[$user_defined_group['role_id']]['member_disallow_posting'] . "', posting_captcha='" . (int)$group_role_permissions[$user_defined_group['role_id']]['posting_captcha'] . "', user_ignore_flood_limit='" . (int)$group_role_permissions[$user_defined_group['role_id']]['user_ignore_flood_limit'] . "', allow_user_sig='" . (int)$group_role_permissions[$user_defined_group['role_id']]['allow_user_sig'] . "', allow_user_sig_bbcode='" . (int)$group_role_permissions[$user_defined_group['role_id']]['allow_user_sig_bbcode'] . "', allow_user_sig_bbcode_img='" . (int)$group_role_permissions[$user_defined_group['role_id']]['allow_user_sig_bbcode_img'] . "', allow_user_sig_bbcode_flash='" . (int)$group_role_permissions[$user_defined_group['role_id']]['allow_user_sig_bbcode_flash'] . "', allow_user_sig_bbcode_url='" . (int)$group_role_permissions[$user_defined_group['role_id']]['allow_user_sig_bbcode_url'] . "', allow_user_sig_smiles='" . (int)$group_role_permissions[$user_defined_group['role_id']]['allow_user_sig_smiles'] . "'");

                //set user defined group permission
                foreach($user_defined_group_permissions[$user_defined_group['role_id']] as $forum_id => $user_defined_group_permission){
                    $this->db->query("INSERT INTO " . DB_PREFIX . "community_permission SET forum_id='" . (int)$forum_id . "', role_id='" . (int)$user_defined_group_permission['role_id'] . "', rank_id='0', view_forum='" . (int)$user_defined_group_permission['view_forum'] . "', topic_view='" . (int)$user_defined_group_permission['topic_view'] . "', topic_publish='" . (int)$user_defined_group_permission['topic_publish'] . "', topic_reply='" . (int)$user_defined_group_permission['topic_reply'] . "', post_delete='" . (int)$user_defined_group_permission['post_delete'] . "', post_edit='" . (int)$user_defined_group_permission['post_edit'] . "', poll_publish='" . (int)$user_defined_group_permission['poll_publish'] . "', poll_edit='" . (int)$user_defined_group_permission['poll_edit'] . "', poll_vote='" . (int)$user_defined_group_permission['poll_vote'] . "', posting_approval='" . (int)$user_defined_group_permission['posting_approval'] . "', posting_bbcode='" . (int)$user_defined_group_permission['posting_bbcode'] . "', posting_bbcode_img='" . (int)$user_defined_group_permission['posting_bbcode_img'] . "', posting_bbcode_flash='" . (int)$user_defined_group_permission['posting_bbcode_flash'] . "', posting_bbcode_url='" . (int)$user_defined_group_permission['posting_bbcode_url'] . "', posting_smiles='" . (int)$user_defined_group_permission['posting_smiles'] . "'");
                }
                
                $role_id=$user_defined_group['role_id'];
            }else{
                //set user defined group role permission
                $this->db->query("INSERT INTO " . DB_PREFIX . "community_role SET role_id='" . (int)$new_role_id_start . "', rank_id='0'");

                //set user defined group permission
                foreach($this->getAllForumIds() as $result){
                    $this->db->query("INSERT INTO " . DB_PREFIX . "community_permission SET forum_id='" . (int)$result['forum_id'] . "', role_id='" . (int)$new_role_id_start . "', rank_id='0'");
                }

                $role_id=$new_role_id_start;
            }

            foreach($user_defined_group['role_title'] as $key => $role_title){
                $this->db->query("INSERT INTO " . DB_PREFIX . "community_role_title SET role_id='" . (int)$role_id . "', language_id='" . (int)$key . "', role_title='" . $this->db->escape(trim($role_title)) . "', role_image='" . $this->db->escape(trim($user_defined_group['role_image'][$key])) . "'");
            }
            
            $new_role_id_start++;
        }

        //pre-defined group
        foreach($data['pre-defined-group'] as $pre_defined_group){
            if($pre_defined_group['role_id']!=5){
                $this->db->query("UPDATE " . DB_PREFIX . "community_role SET member_ban='" . (int)$group_role_permissions[$pre_defined_group['role_id']]['member_ban'] . "', member_warn='" . (int)$group_role_permissions[$pre_defined_group['role_id']]['member_warn'] . "', member_disallow_posting='" . (int)$group_role_permissions[$pre_defined_group['role_id']]['member_disallow_posting'] . "', posting_captcha='" . (int)$group_role_permissions[$pre_defined_group['role_id']]['posting_captcha'] . "', user_ignore_flood_limit='" . (int)$group_role_permissions[$pre_defined_group['role_id']]['user_ignore_flood_limit'] . "', allow_user_sig='" . (int)$group_role_permissions[$pre_defined_group['role_id']]['allow_user_sig'] . "', allow_user_sig_bbcode='" . (int)$group_role_permissions[$pre_defined_group['role_id']]['allow_user_sig_bbcode'] . "', allow_user_sig_bbcode_img='" . (int)$group_role_permissions[$pre_defined_group['role_id']]['allow_user_sig_bbcode_img'] . "', allow_user_sig_bbcode_flash='" . (int)$group_role_permissions[$pre_defined_group['role_id']]['allow_user_sig_bbcode_flash'] . "', allow_user_sig_bbcode_url='" . (int)$group_role_permissions[$pre_defined_group['role_id']]['allow_user_sig_bbcode_url'] . "', allow_user_sig_smiles='" . (int)$group_role_permissions[$pre_defined_group['role_id']]['allow_user_sig_smiles'] . "' WHERE role_id='" . (int)$pre_defined_group['role_id'] . "'");
            }
            
            foreach($pre_defined_group['role_title'] as $key => $role_title){
                $this->db->query("INSERT INTO " . DB_PREFIX . "community_role_title SET role_id='" . (int)$pre_defined_group['role_id'] . "', language_id='" . (int)$key . "', role_title='" . $this->db->escape(trim($role_title)) . "', role_image='" . $this->db->escape(trim($pre_defined_group['role_image'][$key])) . "'");
            }
        }
    }

    public function editUserGlobalPermission($data){
        $sql="UPDATE " . DB_PREFIX . "community_role SET ";

        if($data['role_id']<=3){
            $sql.="member_ban='" . (int)$data['member_ban'] . "', member_warn='" . (int)$data['member_warn'] . "', member_disallow_posting='" . (int)$data['member_disallow_posting'] . "', ";
        }

        $sql.="posting_captcha='" . (int)$data['posting_captcha'] . "', user_ignore_flood_limit='" . (int)$data['user_ignore_flood_limit'] . "', allow_user_sig='" . $data['allow_user_sig'] . "', allow_user_sig_bbcode='" . (int)$data['allow_user_sig_bbcode'] . "', allow_user_sig_bbcode_img='" . $data['allow_user_sig_bbcode_img'] . "', allow_user_sig_bbcode_flash='" . $data['allow_user_sig_bbcode_flash'] . "', allow_user_sig_bbcode_url='" . (int)$data['allow_user_sig_bbcode_url'] . "', allow_user_sig_smiles='" . (int)$data['allow_user_sig_smiles'] . "' WHERE role_id='" . (int)$data['role_id'] . "' AND rank_id='" . (int)$data['rank_id'] . "'";

        $this->db->query($sql);
    }

    public function getMemberRanks(){
        $data=array();

        $query=$this->db->query("SELECT * FROM " . DB_PREFIX . "community_ranks ORDER BY rank_min ASC");

        $i=0;

        foreach ($query->rows as $result) {
            $titleQuery=$this->db->query("SELECT * FROM " . DB_PREFIX . "community_rank_title WHERE rank_id='" . (int)$result['rank_id'] . "' ORDER BY language_id ASC");

            $data[$i]['rank_id']=$result['rank_id'];
            $data[$i]['rank_min']=$result['rank_min'];
            $data[$i]['rank_stars']=$result['rank_stars'];
            $data[$i]['global_permission_edit']=$this->url->link('community/user_group/permission', 'token=' . $this->session->data['token'] . '&rank_id=' . $result['rank_id'] . '&role_id=5', 'SSL');

            foreach($titleQuery->rows as $titleResult){
                $data[$i]['rank_title'][$titleResult['language_id']]=$titleResult['rank_title'];
                $data[$i]['rank_image'][$titleResult['language_id']]=$titleResult['rank_image'];
            }

            $i++;
        }

        return $data;
    }

    public function getPredefinedRoles(){
        $data=array();

        $query=$this->db->query("SELECT * FROM " . DB_PREFIX . "community_role_title WHERE role_id<='7' ORDER BY role_id ASC, language_id ASC");

        foreach($query->rows as $result){
            $data[$result['role_id']]['role_id']=$result['role_id'];
            $data[$result['role_id']]['role_title'][$result['language_id']]=$result['role_title'];
            $data[$result['role_id']]['role_image'][$result['language_id']]=$result['role_image'];
            $data[$result['role_id']]['global_permission_edit']=$this->url->link('community/user_group/permission', 'token=' . $this->session->data['token'] . '&rank_id=0&role_id=' . $result['role_id'], 'SSL');
        }

        return $data;
    }

    public function getUserdefinedRoles(){
        $data=array();

        $query=$this->db->query("SELECT * FROM " . DB_PREFIX . "community_role_title WHERE role_id>'7' ORDER BY role_id ASC, language_id ASC");

        foreach($query->rows as $result){
            $data[$result['role_id']]['role_id']=$result['role_id'];
            $data[$result['role_id']]['role_title'][$result['language_id']]=$result['role_title'];
            $data[$result['role_id']]['role_image'][$result['language_id']]=$result['role_image'];
            $data[$result['role_id']]['global_permission_edit']=$this->url->link('community/user_group/permission', 'token=' . $this->session->data['token'] . '&rank_id=0&role_id=' . $result['role_id'], 'SSL');
        }

        return $data;
    }

    public function getUserGlobalPermission($getData){
        $sql="SELECT r.*, ";

        if(!$getData['rank_id']){
            $sql.="rt.role_title AS title";
        }else{
            $sql.="ra.rank_title AS title";
        }

        $sql.=" FROM " . DB_PREFIX . "community_role AS r ";

        if(!$getData['rank_id']){
            $sql.="LEFT JOIN " . DB_PREFIX . "community_role_title AS rt ON (rt.role_id=r.role_id AND rt.language_id='" . (int)$this->config->get('config_language_id') . "')";
        }else{
            $sql.="LEFT JOIN " . DB_PREFIX . "community_rank_title AS ra ON (ra.rank_id=r.rank_id AND ra.language_id='" . (int)$this->config->get('config_language_id') . "')";
        }

        $sql.=" WHERE r.role_id='" . (int)$getData['role_id'] . "' AND r.rank_id=" . (int)$getData['rank_id'] . " LIMIT 0,1";

        $this->data=array();

        $query=$this->db->query($sql);

        return $query->row;
    }

    private function getAllForumIds(){
        $query=$this->db->query("SELECT forum_id FROM " . DB_PREFIX . "community_forum WHERE forum_type='1'");

        return $query->rows;
    }
}
?>